Search Support (KB)
Article Translations
Other Support Options
|
Mixed C and MASM with MASM Main Language, C Run-Time
Article ID | : | 86816 |
Last Review | : | October 29, 2003 |
Revision | : | 1.1 |
This article was previously published under Q86816
On This Page
SUMMARY
The following steps should be considered when doing
mixed-language programming between the Microsoft Macro Assembler (MASM)
and Microsoft C with MASM as the main language, but the C startup code is
to be brought in to enable the assembly module to call a C run-time
routine:
Note
that the stack declaration comes from the C startup code. The entry point
is in the C startup code, so no entry point should be specified in the
assembler module with the END directive. Doing so will cause only a part
of the necessary initialization to be performed (that is, a stack will be
created, but SS will not get its segment value).
1. | Include the statement
.MODEL <model>, c in the assembly
module to ensure that C naming and calling conventions are used and
that the modules use the same default segments. The <model>
will be small, medium, compact, or large. |
2. | Use the END directive in the assembly module, but do not specify the entry point. |
3. | The start of the assembly module should be labeled
main: and the
label should be made public with the following statement:
-or- main PROC PUBLIC main |
4. | Include the following statement to force a load of
the C startup code:
EXTRN _acrtused:abs |
5. | Assemble the assembly module with /Mx to preserve the case of nonlocal names. If using MASM version 6.0 or later, use /Cx to preserve the case of nonlocal names. |
MORE INFORMATION
The following is a mixed-language example. There is one C
module and one assembly module that must be compiled and then linked
together. No special link options are needed.
The example declares two words, arg1 and arg2, calls the C run-time routine printf to print out their values to the screen, and passes their addresses to a C routine. The C routine swaps the values of arg1 and arg2. The values of arg1 and arg2 are printed out a second time to show that they have been swapped.
The example declares two words, arg1 and arg2, calls the C run-time routine printf to print out their values to the screen, and passes their addresses to a C routine. The C routine swaps the values of arg1 and arg2. The values of arg1 and arg2 are printed out a second time to show that they have been swapped.
Sample Code
/* Compile options needed: none
*/
void ptrswap( int *ptr1, int *ptr2 )
{
int temp;
temp = *ptr1;
*ptr1 = *ptr2;
*ptr2 = temp;
}
----------------------------------------------------------------------
; Assemble options needed: /Mx
.MODEL small, c
.DATA
arg1 DW 1234
arg2 DW 4321
format1 DB "Arg1: %d", 10, 0 ; Format string for printf
format2 DB "Arg2: %d", 10, 10, 0 ; Format string for printf
EXTRN _acrtused:abs ; Bring in C startup
.CODE
EXTRN ptrswap:proc ; External C routine
EXTRN printf:proc ; External C run-time routine
PUBLIC main ; C startup requires the name _main
main:
MOV ax, arg1
PUSH ax ; Push 2nd argument (C convention)
MOV bx, offset format1
PUSH bx ; Push 1st argument (C convention)
CALL printf ; Call C run-time routine
MOV ax, arg2
PUSH ax ; Push 2nd argument (C convention)
MOV bx, offset format2
PUSH bx ; Push 1st argument (C convention)
CALL printf ; Call C run-time routine
MOV bx, offset arg2
PUSH bx ; Push 2nd argument (C convention)
MOV bx, offset arg1
PUSH bx ; Push 1st argument (C convention)
CALL ptrswap ; Call C routine from module
MOV ax, arg1
PUSH ax ; Push 2nd argument (C convention)
MOV bx, offset format1
PUSH bx ; Push 1st argument (C convention)
CALL printf ; Call C run-time routine
MOV ax, arg2
PUSH ax ; Push 2nd argument (C convention)
MOV bx, offset format2
PUSH bx ; Push 1st argument (C convention)
CALL printf ; Call C run-time routine
MOV ah, 4ch ; Terminate program
int 21h
END ; Entry point will be specified by
; the C startup code
APPLIES TO
• | Microsoft Macro Assembler 5.0 |
• | Microsoft Macro Assembler 5.1 Standard Edition |
• | Microsoft Macro Assembler 5.1a |
• | Microsoft Macro Assembler 6.0 Standard Edition |
• | Microsoft Macro Assembler 6.0a |
• | Microsoft Macro Assembler 6.0b |
Keywords: |
KB86816 |